﻿namespace Homework_Snake1015
{
    struct Pos
    {
        public int x;
        public int y;
    }
    internal class Snake
    {
        enum Direction
        {
            Up,
            Down,
            Left,
            Right,
        }

        // 属性与方法
        static public int length;
        public static Pos[] pos;
        Direction dir;
        int width;
        int height;
        ConsoleKeyInfo key;

        public Snake(int width, int height)
        {
            this.width = width;
            this.height = height;
            pos = new Pos[(width - 2) * (height - 2)];
        }

        void Move()
        {
            for (int i = length; i > 0; i--)
            {
                pos[i] = pos[i - 1];
            }

            // 头更新
            switch (dir)
            {
                case Direction.Up:
                    pos[0].y--;
                    break;
                case Direction.Down:
                    pos[0].y++;
                    break;
                case Direction.Left:
                    pos[0].x--;
                    break;
                case Direction.Right:
                    pos[0].x++;
                    break;
            }
        }

        void Grow()
        {
            length++;
        }

        public void Init()
        {
            length = 3;
            dir = Direction.Right;
            pos[0].x = width / 2;
            pos[0].y = height / 2;
        }

        public void Input()
        {
            key = default;
            if (Console.KeyAvailable)
            {
                key = Console.ReadKey(true);
            }
            switch (key.Key)
            {
                case ConsoleKey.W:
                case ConsoleKey.UpArrow:
                    if (dir != Direction.Down)
                    {
                        dir = Direction.Up;
                        break;
                    }
                    break;
                case ConsoleKey.S:
                case ConsoleKey.DownArrow:
                    if (dir != Direction.Up)
                    {
                        dir = Direction.Down;
                        break;
                    }
                    break;
                case ConsoleKey.A:
                case ConsoleKey.LeftArrow:
                    if (dir != Direction.Right)
                    {
                        dir = Direction.Left;
                        break;
                    }
                    break;
                case ConsoleKey.D:
                case ConsoleKey.RightArrow:
                    if (dir != Direction.Left)
                    {
                        dir = Direction.Right;
                        break;
                    }
                    break;
                case ConsoleKey.Q:
                    Grow();
                    break;
            }
        }

        public void Update()
        {
            
        }

        public void FixUpdate()
        {
            
            Move();
        }

        public void Render()
        {
            
            for (int i = 0; i < length + 1; i++)
            {
                // 得到分就变长
                if (Snake.pos[i].x == Food.foodx && Snake.pos[i].y == Food.foody)
                {
                    Grow();
                    Console.SetCursorPosition(2,21);
                    Console.ForegroundColor = ConsoleColor.White;
                    Console.WriteLine("Score:" + (length-3));
                }
                // 碰到墙从另一边出来
                if (pos[i].x == 0)
                {
                    pos[i].x = width - 2;
                }
                else if (pos[i].x == width - 1)
                {
                    pos[i].x = 1;
                }
                if (pos[i].y == 0)
                {
                    pos[i].y = height - 2;
                }
                else if (pos[i].y == height - 1)
                {
                    pos[i].y = 1;
                }
                // 头碰到身体游戏结束
                if (pos[0].x == pos[i+1].x && pos[0].y == pos[i+1].y)
                {
                    End();
                    //Environment.Exit(0);
                }
                // 画蛇
                Console.SetCursorPosition(pos[i].x, pos[i].y);
                Console.ForegroundColor = ConsoleColor.Yellow;
                if (i == 0)
                {
                    Console.WriteLine("@");
                }
                else if (i == length)
                {
                    Console.WriteLine(" ");
                }
                else
                {
                    Console.WriteLine("o");
                }
            }
            
        }

        public void End()
        {
            Console.Clear();
            Console.WriteLine("结束");
            Thread.Sleep(1000000);
        }
    }
}  
